﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// QuickLinkPanel.cs:
//
// Controle que engloba e gerencia QuickLinks
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WBFSSync.AeroControls
{
    public delegate void QuickLinkClickDelegate(QuickLink link);

    public partial class QuickLinksPanel : UserControl
    {
        //----------------------- Eventos

        public event QuickLinkClickDelegate QuickLinkClick;
        public event QuickLinkClickDelegate QuickLinkSpecialClick;
        
        //----------------------- Variáveis

        bool layoutSuspended = false;

        //----------- Construtor
        public QuickLinksPanel()
        {
            InitializeComponent();
        }

        //----------- Quando um Quick link é selecionado, o último selecionado perde a seleção
        public void BroadcastClick(QuickLink link)
        {
            SuspendLayout();

            foreach (Control control in Controls)
            {
                if (control != link) (control as QuickLink).Clicked = false;
            }

            ResumeLayout();
        }

        //----------------- Seleciona um link a partir de um caminho
        public void SelectLink(String path)
        {
            SuspendLayout();

            int matchlength = 0;
            int matchindex = 0;
            for(int i = 0; i < Controls.Count; i++)
            {
                string cpath = ((TagMetadata)Controls[i].Tag).Path;
                if (cpath != "")
                {
                    if (path.Contains(cpath))
                    {
                        if (cpath.Length > matchlength)
                        {
                            (Controls[matchindex] as QuickLink).Clicked = false;
                            (Controls[i] as QuickLink).Clicked = true;
                            matchlength = cpath.Length;
                            matchindex = i;
                        }
                        else
                        {
                            (Controls[i] as QuickLink).Clicked = false;
                        }
                    }
                    else
                    {
                        (Controls[i] as QuickLink).Clicked = false;
                    }
                }
            }

            ResumeLayout();
        }

        //----------------- Override que suspende o layout do controle e dos controles contidos nele
        public new void SuspendLayout()
        {
            base.SuspendLayout();
            layoutSuspended = true;
            foreach (Control c in Controls) c.SuspendLayout();
        }

        //----------------- Override que resume o layout do controle e dos controles contidos nele
        public new void ResumeLayout()
        {
            base.ResumeLayout();
            layoutSuspended = false;
            foreach (Control c in Controls) c.ResumeLayout();
        }

        //----------- Adiciona um quick link
        public void AddQuickLink(int y, String text, TagMetadata meta, Image icon, int level)
        {
            QuickLink ql = new QuickLink(this);

            if (layoutSuspended) ql.SuspendLayout();
            ql.Top = y;
            ql.Text = text;
            ql.Tag = meta;
            ql.Icon = icon;
            ql.Level = level;
            ql.Left = 1;
            ql.Width = Width - 1;
            ql.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

            ql.Click += new EventHandler(ql_Click);
            ql.SpecialClick += new EventHandler(ql_SpecialClick);

            Controls.Add(ql);
        }

        public void AddQuickLink(int y, String text, String tooltip, TagMetadata meta, Image icon, Image special, 
            Image specialOver, int level)
        {
            QuickLink ql = new QuickLink(this);

            if (layoutSuspended) ql.SuspendLayout();
            ql.Top = y;
            ql.Text = text;
            ql.Tag = meta;
            ql.TooltipText = tooltip;
            ql.Icon = icon;
            ql.Level = level;
            ql.SpecialIcon = special;
            ql.SpecialIconOver = specialOver;
            ql.Left = 1;
            ql.Width = Width - 1;
            ql.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

            ql.Click += new EventHandler(ql_Click);
            ql.SpecialClick += new EventHandler(ql_SpecialClick);

            Controls.Add(ql);
        }

        public void AddQuickLink(int y, String text, TagMetadata meta, Image icon, Image special, Image specialOver, int level)
        {
            QuickLink ql = new QuickLink(this);

            if (layoutSuspended) ql.SuspendLayout();
            ql.Top = y;
            ql.Text = text;
            ql.Tag = meta;
            ql.Icon = icon;
            ql.Level = level;
            ql.SpecialIcon = special;
            ql.SpecialIconOver = specialOver;
            ql.Left = 1;
            ql.Width = Width - 1;
            ql.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

            ql.Click += new EventHandler(ql_Click);
            ql.SpecialClick += new EventHandler(ql_SpecialClick);

            Controls.Add(ql);
        }

        void ql_Click(object sender, EventArgs e)
        {
            if (QuickLinkClick != null) QuickLinkClick(sender as QuickLink);
        }

        void ql_SpecialClick(object sender, EventArgs e)
        {
            if (QuickLinkSpecialClick != null) QuickLinkSpecialClick(sender as QuickLink);
        }
    }
}
